<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<%--<script type="text/javascript" src="js/jquery2.js"></script>--%>
<%--<script type="text/javascript" src="js/jquery2.sorted.js"></script>--%>
<script type="text/javascript" src="js/jquery.min.js?v=2.1.4"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/ckform.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<style type="text/css">
body {font-size: 20px;
	padding-bottom: 40px;
	background-color: #e9e7ef;
}

.sidebar-nav {
	padding: 9px 0;
}

@media ( max-width : 980px) {
	/* Enable use of floated navbar text */
	.navbar-text.pull-right {
		float: none;
		padding-left: 5px;
		padding-right: 5px;
	}
}
</style>
</head>
<body>
	<form class="form-inline definewidth m20" action="#" method="get">
	<a href="documentAdd.jsp">发布博客</a>

	</form>
	<table class="table table-bordered table-hover definewidth m10">
		<thead>
			<tr align="center">
				<th>博客标题</th>
				<th>博客类别</th>
				<th>作者</th>
				<th>发布日期</th>
				<th>管理菜单</th>
			</tr>
		</thead>
		<tbody id="tbody">
		   <%--存放行数据--%>
		</tbody>
		<tfoot id="tfoot">
			<%--设置分页信息--%>

		</tfoot>
	</table>
<script type="text/javascript">
	//预加载 -- 页面加载之前执行其中内容
	$(function () {
		//函数调用 -- 展示列表
		showList(1);
	})

	//展示列表
	function showList(currentPage) {
		$.ajax({
			url:"blog/list",
			data:"currentPage="+currentPage,
			dataType:"json",
			success:function (obj) {
				console.log(obj);

				//列表部分
				//清空tbody
				$("#tbody").empty();
				//向tbody添加
				$.each(obj.list,function (i, blog) {
					var typeName = '';
					if(blog.btype == null){
						typeName = '暂无分类';
					}else{
						typeName = blog.btype.typename;
					}
					$("#tbody").append(
						'<tr align="center">'+
							'<td>'+blog.btitle+'</td>'+
							'<td>'+typeName+'</td>'+
							'<td>'+blog.user.uname+'</td>'+
							'<td>'+msToDateString(blog.date)+'</td>'+
							'<td>'+
								'<a href="javascript:deleteInfo('+blog.bid+')">删除 </a>'+
								'&nbsp;&nbsp;&nbsp;&nbsp;'+
								'<a href="blog_detail.jsp?id='+blog.bid+'">博客详情</a>'+
							'</td>'+
						'</tr>'
					);
				});

				var str = "";
				var nums = obj.navigatepageNums;
				for(var i = 0;i < nums.length;i++){
					var num = nums[i];
					if(obj.pageNum == num){
						str += '<a style="color: red;" href="javascript:numPage(' + num + ')">' + num + '</a> ';
					}else{
						str += '<a href="javascript:numPage(' + num + ')">' + num + '</a> ';
					}
				}
				//分页部分
				$("#tfoot").empty();
				$("#tfoot").append(
					'<tr>'+
						'<td colspan="5" style="text-align: center;">'+
							'<a href="javascript:headPage(1)">首页</a>'+
							'<a href="javascript:prePage('+obj.prePage+')">上一页</a>'+
							'[ ' + str + ']'+
							'<a href="javascript:nextPage('+obj.nextPage+')">下一页</a>'+
							'<a href="javascript:tailPage('+obj.pages+')">尾页</a>'+
						'</td>'+
					'</tr>'
				)
			}
		});
	}

	//函数 -- 毫秒值和日期字符串转换 把毫秒值转成日期字符串
	function msToDateString(ms) {
		var date = new Date(ms);
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();

		if(month <= 9){
			month = "0" + month;
		}

		if(day <= 9){
			day = "0" + day;
		}

		return year + "-" + month + "-" + day;
	}

	//首页函数
	function headPage(page) {
		showList(page);
	}

	//尾页函数
	function tailPage(page) {
		showList(page);
	}

	//上一页函数
	function prePage(page) {
		showList(page);
	}

	//下一页函数
	function nextPage(page) {
		showList(page);
	}

	//页码数字函数
	function numPage(page) {
		showList(page);
	}

	//删除博客
	function deleteInfo(id) {
		if(confirm("是否要删除当前博客？")){
			$.ajax({
				url:"blog/delete",
				data:"id="+id,
				type:"get",
				dataType:"json",
				success:function (obj) {
					console.log(obj);
					if(obj.flag){
						alert(obj.message);
						location.reload();
					}
				}
			});
		}else{
			alert("取消了删除操作");
		}
	}
</script>
</body>
</html>
